package com.ziq.zcamp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.ziq.zcamp.R;
import com.ziq.zcamp.manager.GlobalContextManager;

import java.util.ArrayList;
import java.util.List;

public class TemperatureLineView extends FrameLayout {
    Paint linePaint = new Paint();
    Paint imagePaint = new Paint();
    Paint textPaint = new Paint();
    float baseLineDistance = 0;
    //抗锯齿
    PaintFlagsDrawFilter paintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
    ArrayList<float[]> tempMaxPointList = new ArrayList<>();
    ArrayList<float[]> tempMinPointList = new ArrayList<>();
    List<WeatherDailyBean.DailyBean> dateList;
    int itemTop = 0;
    int itemHeight= 0;
    int textSize = 0;
    int labelHeight = 0;
    int labelWidth = 0;
    int imageHeight = 0;
    int imageWidth = 0;
    int imageMarginRight = 0;
    int tempMax = 0;
    int tempMin = 0;
    int tempLevelCount = 0;
    Bitmap tempHighBitmap;
    Rect tempHighSrc;
    Bitmap tempLowBitmap;
    Rect tempLowSrc;
    public TemperatureLineView(Context context) {
        this(context, null);
    }

    public TemperatureLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TemperatureLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        tempHighBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_temperature_high);
        tempHighSrc = new Rect(0,0,tempHighBitmap.getWidth(),tempHighBitmap.getHeight());
        tempLowBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_temperature_low);
        tempLowSrc = new Rect(0,0,tempLowBitmap.getWidth(),tempLowBitmap.getHeight());

        imageHeight = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_3);
        imageWidth = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_6);
        imageMarginRight = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_2);


        labelHeight = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_14);
        labelWidth = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_12);
        itemTop = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_119) + labelHeight;
        itemHeight = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_105) - labelHeight;

        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeCap(Paint.Cap.ROUND);
        linePaint.setStrokeWidth(GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_dip_1));
        linePaint.setColor(0xffD8D5D3);

        textPaint.setColor(0xff6E5F55);
        textSize = GlobalContextManager.INSTANCE.getResources().getDimensionPixelSize(R.dimen.dimens_sp_10);
        textPaint.setTextSize(textSize);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
        float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
        baseLineDistance = (bottom - top) / 2 - bottom;
        setWillNotDraw(false);
    }

    public void setWeatherData(List<WeatherDailyBean.DailyBean> dateList){
        this.dateList = dateList;


        if(dateList != null && !dateList.isEmpty()){
            for(int i = 0; i < dateList.size(); i++){
                WeatherDailyBean.DailyBean dailyBean = dateList.get(i);
                int max = Integer.parseInt(dailyBean.getTempMax());
                int min = Integer.parseInt(dailyBean.getTempMin());
                if(max > tempMax || i == 0 ){
                    tempMax = max;
                }
                if(min < tempMin || i == 0 ){
                    tempMin = min;
                }
            }
        }
        tempLevelCount = tempMax - tempMin;
        requestLayout();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(paintFlagsDrawFilter);
        if(dateList != null && dateList.size() >= 2){
            int length = dateList.size();
            int canvasWidth = canvas.getWidth();
            int itemWidth = canvasWidth / length;
            tempMaxPointList.clear();
            tempMinPointList.clear();
            for(int i = 0; i < length; i++){
                WeatherDailyBean.DailyBean dailyBean = dateList.get(i);
//                int left = itemWidth * i + 1;
//                int top = itemTop;
//                int right = left + itemWidth;
//                int bottom = top + itemHeight;
//                Rect rect = new Rect(left, top, right, bottom);
//                canvas.drawRect(rect, linePaint);

                int pointLeft = itemWidth * i + 1 + itemWidth / 2;
                int pointTopForMax = itemTop + (itemHeight / tempLevelCount) * (tempLevelCount - (Integer.parseInt(dailyBean.getTempMax()) - tempMin));
                int pointTopForMin = itemTop + (itemHeight / tempLevelCount) * (tempLevelCount - (Integer.parseInt(dailyBean.getTempMin()) - tempMin));
                tempMaxPointList.add(new float[]{pointLeft, pointTopForMax});
                tempMinPointList.add(new float[]{pointLeft, pointTopForMin});
            }
            for(int i = 0; i < length; i++){
                WeatherDailyBean.DailyBean dailyBean = dateList.get(i);
                if(i < length - 1){
                    canvas.drawLine(tempMaxPointList.get(i)[0], tempMaxPointList.get(i)[1], tempMaxPointList.get(i+1)[0], tempMaxPointList.get(i+1)[1], linePaint);
                    canvas.drawLine(tempMinPointList.get(i)[0], tempMinPointList.get(i)[1], tempMinPointList.get(i+1)[0], tempMinPointList.get(i+1)[1], linePaint);
                }
                canvas.drawText(dailyBean.getTempMax(), tempMaxPointList.get(i)[0], tempMaxPointList.get(i)[1] - baseLineDistance, textPaint);
                canvas.drawText(dailyBean.getTempMin(), tempMinPointList.get(i)[0], tempMinPointList.get(i)[1] - baseLineDistance, textPaint);
                int tempHighBitmapLeft = (int)tempMaxPointList.get(i)[0] - imageWidth  - imageMarginRight;
                int tempHighBitmapTop = (int)tempMaxPointList.get(i)[1] - imageHeight - (int)baseLineDistance - (textSize/2 - imageHeight);
                canvas.drawBitmap(tempHighBitmap, tempHighSrc, new Rect(tempHighBitmapLeft, tempHighBitmapTop, tempHighBitmapLeft + imageWidth, tempHighBitmapTop + imageHeight), imagePaint);

                int tempLowBitmapLeft = (int)tempMinPointList.get(i)[0] - imageWidth - imageMarginRight;
                int tempLowBitmapTop = (int)tempMinPointList.get(i)[1] - imageHeight - (int)baseLineDistance  - (textSize/2 - imageHeight);
                canvas.drawBitmap(tempLowBitmap, tempLowSrc, new Rect(tempLowBitmapLeft, tempLowBitmapTop, tempLowBitmapLeft + imageWidth, tempLowBitmapTop + imageHeight), imagePaint);
            }
        }
    }
}